package com.generator.purchaseManage.productManage.controller;

import com.generator.aboutUser.user.pojo.Users;
import com.generator.purchaseManage.brandManage.pojo.CBrand;
import com.generator.purchaseManage.brandManage.service.impl.BrandService;
import com.generator.purchaseManage.productManage.pojo.CProduct;
import com.generator.purchaseManage.productManage.service.impl.ProductService;
import com.generator.purchaseManage.productTypeManage.pojo.CType;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/06
 * 星期六 17:50
 */
@Controller
@RequestMapping("/productManage")
public class ProductController {

    @Autowired
    private ProductService productService;

    public ProductService getProductService() {
        return productService;
    }
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    @Autowired
    private BrandService brandService;

    public BrandService getBrandService() {
        return brandService;
    }
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }


    /**
     * 根据查询条件查询商品信息
     * @param product
     * @return
     */
    @RequestMapping("/getProductByCondition")
    public String getProductByCondition(PageModel<CProduct> pageModel, CProduct product, Model model){
        System.out.println("前端传来的查询条件"+product);
        PageModel<CProduct> productList = productService.queryProductByCondition(product, pageModel);
        model.addAttribute("productList",productList);
        return "purchase/product/productList";
    }


    /**
     * 添加商品
     * @param product
     * @return
     */
    @ResponseBody
    @RequestMapping("/addProduct")
    public boolean addProduct(CProduct product,HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user != null){
            product.setCreateName(user.getUname());
            System.out.println("要添加的商品信息"+product);
        }
        return productService.addProduct(product);
    }

    /**
     * 跳转到修改修改商品信息页面
     * @param product
     * @return
     */
    @RequestMapping("/toUpdateProductView/{typeName}/{productModel}")
    public String toUpdateProductView(CProduct product,HttpSession session,Model model){
        // 根据商品类型名查询到商品类型信息
        CProduct updateProduct = productService.queryProductByName(product);
        model.addAttribute("updateProduct",updateProduct);
        // 把当前修改的商品类型信息放入session供updateProduct方法使用
        session.setAttribute("updateProduct",updateProduct);
        return "purchase/product/productUpdate";
    }

    /**
     * 修改商品信息
     * @param product
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateProduct")
    public boolean updateProduct(CProduct product,HttpSession session){
        CProduct updateProduct = (CProduct) session.getAttribute("updateProduct");
        if (product != null){
            // 设置要修改的商品ID
            product.setProductId(updateProduct.getProductId());
            return productService.updateProduct(product);
        }
        return false;
    }

    /**
     * 删除商品信息
     * @param product
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteProduct")
    public boolean deleteProduct(CProduct product){
        if (product != null){
            System.out.println("前端拿到的数据"+product);
            CProduct cProduct = productService.queryProductByName(product);
            System.out.println("要删除的商品"+cProduct);
            return productService.deleteProduct(cProduct);
        }
        return false;
    }



}
